/*

@控制英雄--游戏操作

*/

//用来检测键盘的状态
var isLeft=0;
var isRight=0;
var isUp=0;
var isDown=0;

//用来记录touchstart的初始坐标
var tx=0;
var ty=0;
var heroX=0;
var heroY=0;

if(isPC()){
    //PC端
    window.onkeydown=function(event){
        var e =event||window.event;
        switch(e.keyCode){
            case 37 :
                isLeft=1;
                break;
            case 38 :
                isUp=1;
                break;
            case 39 :
                isRight=1;
                break;
            case 40 :
                isDown=1;
                break;
            default :
                break;
        }
    }
    window.onkeyup=function(event){
        var e =event||window.event;
        switch(e.keyCode){
            case 37 :
                isLeft=0;
                break;
            case 38 :
                isUp=0;
                break;
            case 39 :
                isRight=0;
                break;
            case 40 :
                isDown=0;
                break;
            case 32 :
                //这里是炸弹
                if(bombNum!=0){
                    bombNum--;
//                    for循环用来计算得分，需加班
                    for(var i=0;i<enemys.length;i++){
                        score+=enemys[i].score;
                        p.innerHTML="得分："+score+"分";
                    }
                    enemys=[];
                }
                break;
            default :
                break;
        }
    }
    but.onclick=function (){
        again();
    };
    
} else {
    //移动端
    window.addEventListener("touchstart",function(e){
        e.preventDefault();
        var touches=e.touches;
        if(touches.length==1){
            var touch=touches[0];
            tx=touch.clientX;
            ty=touch.clientY;
            heroX=hero.x;
            heroY=hero.y;
            if(tx>hero.x&&tx<hero.x+hero.w&&ty>hero.y&&ty<hero.y+hero.h){
                //点中小飞机
                window.addEventListener("touchmove",move,false)
            }
        } else if(touches.length==2){
            //bomb
            if(bombNum!=0){
                bombNum--;
//            for循环用来计算得分，需加班
              for(var i=0;i<enemys.length;i++){
                  score+=enemys[i].score;
                  p.innerHTML="得分："+score+"分";
               }
            enemys=[];
            }
        }
    },false);
    window.addEventListener("touchend",function(){
        window.removeEventListener("touchmove",move);
    },false);
    but.addEventListener("touchstart",again,false);
    but.addEventListener("touchend",function(){
        but.removeEventListener("touchmove",again);
    },false);
}

//用来完成键盘的事情
function control(){
    if(isLeft){
        hero.x-=7;
        if(hero.x<-hero.w/2){
            hero.x=-hero.w/2;
        }
    }
    if(isRight){
        hero.x+=7;
        if(hero.x>canvas.width-hero.w/2){
            hero.x=canvas.width-hero.w/2;
        }
    }
    if(isUp){
        hero.y-=7;
        if(hero.y<0){
            hero.y=0;
        }
    }
    if(isDown){
        hero.y+=7;
        if(hero.y>canvas.height-hero.h){
            hero.y=canvas.height-hero.h;
        }
    }
}

function move(e){
    e.preventDefault();
    var touches=e.touches;
    var touch=touches[0];
    var x=touch.clientX;
    var y=touch.clientY;
    hero.x=heroX+x-tx;
    hero.y=heroY+y-ty;
}
//再次开始
function again(){
    div.style.display='none';
    p.style.display='block';
    score=0
    gameStart();
    hero.isBol=false;
    hero.isOver=false;
    hero.showIndex=0;
    hero.x=canvas.width/2-33;
    hero.y=canvas.height-82;
    gap=7;
    gapNum=0;
    bullets=[]; 
    isDouble=false;
    isCrowed=false;
    tx=0;
    ty=0;
    heroX=0;
    heroY=0;
    eNum=250;
    p.innerHTML="得分：0分";
}




